import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
/**
 * 
 * @author Jiri Hodan
 *
 */
public class BoardPanel extends JPanel {
	private static final long serialVersionUID = -3238535282174725158L;
	
	private static int leftTopX = 0;

	private static int leftTopY = 0;

	private int boardSize = 0;
	private Graphics2D g2d;
	private Image emptyImg = new ImageIcon("images/BoardImage.png").getImage();
	private Image whiteImg = new ImageIcon("images/WhitePlayerImage.png").getImage();
	private Image blackImg = new ImageIcon("images/BlackPlayerImage.png").getImage();
	private static int imageSize = 0;
	private Board b = new Board();
	private int mouseX = -1, mouseY = -1;
	
	/**
	 * Konstruktor
	 */
	public BoardPanel() {
		addMouseListener(new MouseClass());
		
	}
	
	/**
	 * Vykresli BoardPanel
	 */
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g2d = (Graphics2D)g;
		gobanSize();
		//g.fillRect(getLeftTopX(), getLeftTopY(), getBoardSize(), getBoardSize());
		drawBoard();
		drawPlayerMove();
	}
	
	/**
	 * prizpusobi velikost Gobanu velikosti panelu
	 */
	public void gobanSize(){
		int size = 0;
		
		if(getSize().width <= getSize().height){
			size = ((getSize().width - 20)/Board.BOARD_SIZE)*Board.BOARD_SIZE;
		}else{
			size = ((getSize().height - 20)/Board.BOARD_SIZE)*Board.BOARD_SIZE;
		}
		
		setBoardSize(size);
		gobanLeftTopCorner(getBoardSize());
	}
	
	/**
	 * vykresli Goban
	 */
	public void drawBoard(){
		imageSize = getBoardSize()/Board.BOARD_SIZE;
		for(int i = 0; i < Board.BOARD_SIZE; i++){
			for(int j = 0; j < Board.BOARD_SIZE; j++){
				g2d.drawImage(emptyImg, getLeftTopX() + i*imageSize, getLeftTopY() + j*imageSize, imageSize, imageSize, this);
			}
		}
			
	}
	
	/**
	 * vykresli polozene kameny
	 */
	public void drawPlayerMove(){
		for(int i = 0; i < Board.BOARD_SIZE; i++){
			for(int j = 0; j < Board.BOARD_SIZE; j++){
				if(Board.getBoard()[i][j] == 1){
					drawBlackPlayerMove(i * imageSize, j * imageSize);
				}else if(Board.getBoard()[i][j] == 2){
					drawWhitePlayerMove(i * imageSize, j * imageSize);
				}
			}
		}
	}
	
	/**
	 * vykresli cerny kamen
	 * @param x
	 * @param y
	 */
	private void drawBlackPlayerMove(int x, int y){
		int velikostPolicka = getBoardSize()/Board.BOARD_SIZE;
		try{
			g2d.drawImage(blackImg, x/velikostPolicka*velikostPolicka + getLeftTopX(), y/velikostPolicka*velikostPolicka + getLeftTopY(), velikostPolicka, velikostPolicka, this);
		}catch(ArithmeticException e){
			
		}
	
	}

	/**
	 * vykresli bily kamen
	 * @param x
	 * @param y
	 */
	private void drawWhitePlayerMove(int x, int y){
		int velikostPolicka = getBoardSize()/Board.BOARD_SIZE;
		try{
			g2d.drawImage(whiteImg, x/velikostPolicka*velikostPolicka + getLeftTopX(), y/velikostPolicka*velikostPolicka + getLeftTopY(), velikostPolicka, velikostPolicka, this);
		}catch(ArithmeticException e){
			
		}
	}
	
	
	/**
	 * zajisti vykresleni Gobanu na stred
	 * @param size
	 */
	private void gobanLeftTopCorner(int size){
		setLeftTopX((int) ((getSize().getWidth() - size)/2));
		setLeftTopY((int) ((getSize().getHeight() - size)/2));
	}

	public static int getLeftTopX() {
		return leftTopX;
	}

	public void setLeftTopX(int leftTopX) {
		BoardPanel.leftTopX = leftTopX;
	}

	public static int getLeftTopY() {
		return leftTopY;
	}

	public void setLeftTopY(int leftTopY) {
		BoardPanel.leftTopY = leftTopY;
	}

	public int getBoardSize() {
		return boardSize;
	}

	public void setBoardSize(int boardSize) {
		this.boardSize = boardSize;
	}	
	
	public static int getImageSize() {
		return imageSize;
	}

	public static void setImageSize(int imageSize) {
		BoardPanel.imageSize = imageSize;
	}

	/**
	 * 
	 * @author Jiri Hodan
	 *
	 */
	private class MouseClass extends MouseAdapter{
		/**
		 * metoda, ktera zpracovava spusteni klavesy
		 */
		public void mouseReleased(MouseEvent e) {
			if (e.getX() > BoardPanel.getLeftTopX()
					&& e.getY() > BoardPanel.getLeftTopY()) {
				mouseX = (e.getX() - BoardPanel.getLeftTopX())
						/ BoardPanel.getImageSize();
				mouseY = (e.getY() - BoardPanel.getLeftTopY())
						/ BoardPanel.getImageSize();
			}
			
			if(Move.isWin() == false && Move.draw == false){	
				Move.play(mouseX, mouseY);
			}else{
				b.newGame();
			}
			
			repaint();
		}

	}
	
}


